<?php

/**
 * @filename TopicController.php 
 * @encoding UTF-8 
 * @author Liangrong Li <liliangrong0@126.com> 
 * @datetime 2016-8-16  11:36:23
 * @Description 
 */

namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use common\models\Download;
use common\models\Manual;
use common\models\Post;
use common\models\Video;
use common\components\CareerHelper;
use yii\web\NotFoundHttpException;

class TopicController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    //  话题Topic 首页.
    public function actionIndex($slug) {
        //  根据slug查询ID、title
        $career = CareerHelper::CareerSlug($slug);
        if (isset($career)) {
            $title = $career['title'];
            $cid = $career['cid'];
            //  查询该cid下各区块内容的tags
            $download_tags = Download::find()->select('tag')->where(['cid' => $cid])->orderBy(['updated_at' => SORT_DESC])->asArray()->all();
            $post_tags = Post::find()->select('tag')->where(['cid' => $cid])->orderBy(['updated_at' => SORT_DESC])->asArray()->all();

            return $this->render('index', [
                        'download_tags' => $download_tags,
                        'post_tags' => $post_tags,
                        'slug' => $slug,
                        'title' => $title,
                        'cid' => $cid,
            ]);
        } else {
            throw new NotFoundHttpException(Yii::t('common', 'NotFound'));
        }
    }

    /**
     * 话题Topic 列表页.
     * @return mixed
     */
    public function actionList($slug, $name) {
        //  根据slug查询ID、title
        $career = CareerHelper::CareerSlug($slug);
        if (($career != null) & ($name != null)) {
            $title = $career['title'];
            $cid = $career['cid'];
            //  根据cid查询 manual 模块，首个 LIKE 用 andFilterWhere
            $midQuery = "SELECT * FROM `manual` WHERE `mid` in (select `mid` from "
                    . "`manuals` where `cid` = {$cid}) and `title` like '%{$name}%'";
            $manuals = Manual::findBySql($midQuery)
                            ->orderBy(['updated_at' => SORT_DESC])
                            ->asArray()->all();
            
            //  根据cid查询 post 模块，首个 LIKE 用 andFilterWhere
            $pidQuery = "SELECT * FROM `post` WHERE `pid` in (select `pid` from `posts` "
                    . "where `cid` = {$cid}) and `title` like '%{$name}%' or `tag` like '%{$name}%'";
            $posts = Post::findBySql($pidQuery)
                            ->orderBy(['updated_at' => SORT_DESC])
                            ->asArray()->all();
            
            //  根据cid查询 video 模块，首个 LIKE 用 andFilterWhere
            $vidQuery = "SELECT * FROM `video` WHERE `vid` in (select `vid` from `videos` "
                    . "where `cid` = {$cid}) and `title` like '%{$name}%' or `tag` like '%{$name}%'";
            $videos = Video::findBySql($vidQuery)
                            ->orderBy(['updated_at' => SORT_DESC])
                            ->asArray()->all();

            //  根据cid查询post模块
//            $posts = Post::find()->where(['cid' => $cid])
//                            ->andFilterWhere(['like', 'title', $name])
//                            ->orFilterWhere(['like', 'tag', $name])
//                            ->orderBy(['updated_at' => SORT_DESC])  //按发布时间倒序排列
//                            ->asArray()->all();
            //  返回数据
            return $this->render('list', ['manuals' => $manuals,
                        'posts' => $posts,
                        'videos' => $videos,
                        'name' => $name,
                        'slug' => $slug,
                        'title' => $title,
                        'cid' => $cid,
            ]);
        } else {
            throw new NotFoundHttpException(Yii::t('common', 'NotFound'));
        }
    }

}
